/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package neo.workforce.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import neo.workforce.dao.Transaction;
import neo.workforce.dao.TransactionDAO;
import neo.workforce.dao.Workspace;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

/**
 *
 * @author KTB_User
 */
public class TransactionDAOImpl implements TransactionDAO {

    private Logger log = Logger.getLogger(this.getClass());
    private JdbcTemplate jdbcTemplate = null;
    
    public TransactionDAOImpl() {
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    public Transaction findTransaction(final long id) {
        final StringBuilder sql = new StringBuilder();
        sql.append("SELECT ID, SESSION_ID, PROCESS_ID, ACTIVITY_ID, USER_ID, ");
        sql.append("IN_STATE, OUT_STATE, STATE_TYPE, CREATE_DATE, UPDATE_DATE FROM TRANSACTIONS WHERE ID = ").append(id);
        log.info("SQL:"+ sql.toString());
        Transaction trans = null;
        try {
            trans = (Transaction)jdbcTemplate.queryForObject(sql.toString(), new TransactionRowMapper());
            return trans;
        } catch(Exception ex) {
            log.info("findTransaction Error:", ex);
            return null;
        }
    }
    
    
    
    public long saveTransaction(final Transaction trans) {
        final StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO TRANSACTIONS(USER_ID, PROCESS_ID, ACTIVITY_ID, SESSION_ID, ");
        sql.append("IN_STATE, OUT_STATE, STATE_TYPE, CREATE_DATE, UPDATE_DATE) VALUES(?,?,?,?,?,?,?,?)");
        log.info("SQL:"+ sql.toString());
        KeyHolder keyHolder = new GeneratedKeyHolder();
        try {
            jdbcTemplate.update(
                    new PreparedStatementCreator() {
                public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                    PreparedStatement ps =
                            connection.prepareStatement(sql.toString());
                    ps.setLong(1, trans.getUserId());
                    ps.setLong(2, trans.getProcessId());
                    ps.setLong(3, trans.getActivityId());
                    ps.setString(4, trans.getSessionId());
                    ps.setString(5, trans.getInState());
                    ps.setString(6, trans.getOutState());
                    ps.setString(7, trans.getStateType());
                    ps.setDate(8, new java.sql.Date(trans.getCreateDate().getTime()));
                    ps.setDate(9, new java.sql.Date(trans.getUpdateDate().getTime()));
                    return ps;
                }
             }, keyHolder);
            return keyHolder.getKey().longValue();
        } catch (Exception ex) {
            log.info("save Transaction:", ex);
            return -1;
        }
    }
    
    public boolean updateTransaction(final Transaction trans) {
        final StringBuilder sql = new StringBuilder();
        sql.append("UPDATE TRANSACTIONS SET USER_ID =?, PROCESS_ID = ?, ACTIVITY_ID = ?, SESSION_ID = ?, ");
        sql.append("IN_STATE=?, OUT_STATE=?, STATE_TYPE=?, UPDATE_DATE = ? WHERE ID = ?");
        try {
            jdbcTemplate.update(sql.toString(), new PreparedStatementCreator() {
                public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                    PreparedStatement ps =
                            connection.prepareStatement(sql.toString());
                    ps.setLong(1, trans.getUserId());
                    ps.setLong(2, trans.getProcessId());
                    ps.setLong(3, trans.getActivityId());
                    ps.setString(4, trans.getSessionId());
                    ps.setString(5, trans.getInState());
                    ps.setString(6, trans.getOutState());
                    ps.setString(7, trans.getStateType());
                    ps.setDate(8, new java.sql.Date(trans.getCreateDate().getTime()));
                    ps.setLong(9, trans.getId());
                    return ps;
                }
            });
            return true;
        } catch(Exception ex) {
            log.info("update Transaction:", ex);
            return false;
        }
    }
    
    
}
